/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controller;

import DAOs.CollaboratorJpaController;
import Entity.Collaborator;
import Entity.Visit;
import java.util.Calendar;
import java.util.List;
import javax.persistence.EntityManagerFactory;

/**
 *
 * @author USUARIO
 */
public class VisitController {
    private InputController inputController;
    private EntityManagerFactory emf;
    public VisitController(EntityManagerFactory emf){
        this.emf = emf;
        inputController = new InputController();
    }
    
    public String findCollaborator(String nit){
        String colaboratorString = "";
        if(inputController.isInteger(nit)){
            String visitas = "Fecha\tHora Inicio\tHora Fin\tZona\n";
            CollaboratorJpaController cJPA = new CollaboratorJpaController(emf);
            List<Collaborator> cList = cJPA.findCollaboratorEntities();
            Collaborator c = null;
            for(Collaborator u : cList){
                if(u.getCedula() == Integer.parseInt(nit)){
                    c = u;
                }
            }
            if(c != null){
                for(Visit v : c.getVisits()){
                    visitas +=  v.getFecha().get(Calendar.DAY_OF_MONTH) + "/" + (v.getFecha().get(Calendar.MONTH)+1) + ""
                        + "/"+ v.getFecha().get(Calendar.YEAR) + "\t" + v.getHoraInicio() + "\t" + v.getHoraFin() + v.getZoneID()
                        + "\n";
                }
        
                colaboratorString = "Name: " + c.getName() + " " + c.getLastname()
                    + "\nCedula: " + c.getCedula()
                    + "\nAge: " + c.getAge()
                    + "\nVisitas: "
                    + visitas;
            }else{
                colaboratorString = "No existe el colaborador";
            }
        }
        else{
            colaboratorString = "Ingrese un numero válido";
        }
        return colaboratorString;
    }
    
    public boolean createVisitPossible(Calendar dateOfVisit, int beginHour, int endHour, String nit){        
        boolean possible = true;
        if(inputController.isInteger(nit)){
            CollaboratorJpaController cJPA = new CollaboratorJpaController(emf);
            List<Collaborator> cList = cJPA.findCollaboratorEntities();
            Collaborator c = null;
            for(Collaborator u : cList){
                if(u.getCedula() == Integer.parseInt(nit)){
                    c = u;
                }
            }
            if(c != null){
                for(Visit v : c.getVisits()){
                    if(v.getFecha().equals(dateOfVisit) && ((v.getHoraInicio() < beginHour && v.getHoraFin() > beginHour)
                            || (v.getHoraInicio() < endHour && v.getHoraFin() > endHour))){
                        possible = false;
                    }
                }
            }else{
                possible = false;
            }
        }else{
            possible = false;
        }
        Calendar actual = Calendar.getInstance();
        if(dateOfVisit.get(Calendar.YEAR) >= actual.get(Calendar.YEAR)){
            if(dateOfVisit.get(Calendar.DAY_OF_YEAR) > actual.get(Calendar.DAY_OF_YEAR)){
                
            }else{
                possible = false;
            }
        }else{
            possible = false;
        }
        
        return possible;
    }
}
